package com.castrianni.cvs;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataConstants;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vcs.AbstractVcs;
import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.ProjectLevelVcsManager;
import com.intellij.openapi.vcs.actions.VcsContextFactory;
import com.intellij.openapi.vcs.history.VcsHistoryProvider;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.peer.PeerFactory;

public class CVSRevisionGraphAction extends AnAction {
  public void actionPerformed(AnActionEvent ae)
  {
    Project project = null;
    VirtualFile file = null;
    try {
      project = (Project)ae.getDataContext().getData(DataConstants.PROJECT);
      file = (VirtualFile)ae.getDataContext().getData(DataConstants.VIRTUAL_FILE);
      ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(project);
      AbstractVcs vcs = vcsManager.getVcsFor(file);
      VcsHistoryProvider histProvider = vcs.getVcsHistoryProvider();
      PeerFactory peerFactory = PeerFactory.getInstance();
      VcsContextFactory vcsContext = peerFactory.getVcsContextFactory();
      FilePath filePath = vcsContext.createFilePathOn(file);
      CVSRevisionGraph dialog = new CVSRevisionGraph(project,filePath,histProvider);
      dialog.pack();
      dialog.show();
    } catch(Throwable t) {
      t.printStackTrace();
      String title = "Revision Graph Error";
      String message;
      if(file != null)
        message = "Error obtaining information necessary to calculate revision graph for file: " + file.getName();
      else
        message = "Error obtaining information necessary to calculate revision graph for current file";
      message += "\n\n" + t.getMessage();
      if(project != null)
        Messages.showErrorDialog(project,message,title);
      else
        Messages.showErrorDialog(message,title);
    }
  }
}
